import request from '@/utils/request'
import type { LikeType } from '@/types/consult'

import type {
  ConsultParams,
  ConsultResData,
  PartialConsult,
  CommonParams,
  DocResData,
  Deps,
  Image,
  ConsultOrderParams,
  ConsultOrderData,
  ConsultOrderItem,
  ConsultOrderPage,
  ConsultOrderListParams
} from '@/types/consult'
import type { Patient } from '@/types/user'

// 查询列表接口
export const getConsultList = (params: ConsultParams) =>
  request<ConsultResData>('/patient/home/knowledge', 'get', params)

// 查询医生列表
export const getDocList = (params: CommonParams) =>
  request<DocResData>('/home/page/doc', 'get', params)

// 关注接口
export const like = (id: string, type: LikeType = 'doc') =>
  request('/like', 'post', { id, type })

// 获取所有的科室
export const getAllDeps = () => request<Deps[]>('/dep/all')

// 上传病情的图片
export const uploadImg = (file: File) => {
  // 构建一个formData格式的数据专门用来上传文件
  const formData = new FormData()
  // 向formData中添加数据
  formData.append('file', file)
  return request<Image>('/upload', 'POST', formData)
}

// 获取患者详情
export const getPatientDetailById = (id: string) =>
  request<Patient>(`patient/info/${id}`)

// 获取就诊信息
export const getConsultOrderPre = (obj: ConsultOrderParams) =>
  request<ConsultOrderData>('/patient/consult/order/pre', 'GET', obj)

// 生成订单
export const createConsultOrder = (consult: PartialConsult) =>
  request<{ id: string }>('/patient/consult/order', 'POST', consult)

// 支付订单
export const payConsultOrder = (obj: {
  paymentMethod: 0 | 1
  orderId: string
  payCallback: string
}) => request<{ payUrl: string }>('patient/consult/pay', 'POST', obj)

// 获取订单详情
export const getConsultOrderDetail = (id: string) =>
  request<ConsultOrderItem>('/patient/consult/order/detail', 'GET', {
    orderId: id
  })

// 获取处方图片
export const getChufangImg = (id: string) =>
  request<{ url: string }>(`/patient/consult/prescription/${id}`)

// 给医生评价
export const evaluateDoctor = (obj: {
  docId: string
  orderId: string
  score: number
  content: string
  anonymousFlag: number
}) => request<{ id: string }>('/patient/order/evaluate', 'post', obj)

// 获取问诊订单列表
export const getConsultOrderList = (obj: ConsultOrderListParams) =>
  request<ConsultOrderPage>('/patient/consult/order/list', 'get', obj)

// 取消问诊订单
export const cancelOrderById = (id: string) =>
  request(`/patient/order/cancel/${id}`, 'PUT')

// 删除订单
export const deleteOrderById = (id: string) =>
  request(`/patient/order/${id}`, 'DELETE')
